// pages/confirm-order/confirm-order.js
import Dialog from 'vant-weapp/dialog/dialog';
import Toast from 'vant-weapp/toast/toast';
const app = getApp();
const http = app.http;
const userInfoManager = app.userInfoManager;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    color: '#000000',
    background: '#ffffff',
    confirmShow: false,
    personInfo: {},
    heightLine: "",
    viewHeight: "",
    currentOrderData: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(app.globalData.statusBarHeight);
    let heightLine = (app.globalData.statusBarHeight + 44) * 2;
    let viewHeight = heightLine + 391;
    this.setData({
      heightLine,
      viewHeight
    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let personInfo = userInfoManager.getSupplierInfo();
    this.data.personInfo = personInfo;
    this.initData();
  },
  /**初始化数据 */
  initData() {
    const toast = Toast.loading({
      duration: 0, // 持续展示 toast
      forbidClick: true, // 禁用背景点击
      message: '加载中...',
      loadingType: 'spinner',
      selector: '#van-toast'
    });
    http.getOrderDettails(this.data.personInfo.id, 1).then(res => {
      if (res.code == 0) {
        for (var i = 0; i < res.data.length; i++) {
          res.data[i].endTime = res.data[i].arrivalDateEnd.split(" ")[1];
          res.data[i].productslen = Object.getOwnPropertyNames(res.data[i].products).length;
        }
        this.setData({
            orderList: res.data
          },
          function() {
            Toast.clear();
          });
      } else {
        wx.showToast({
          duration: 1000,
          icon: 'none',
          title: res.msg
        })
      }
    })
  },
  clickSure(event) {
    console.log(event);
    this.data.currentOrderData = event.currentTarget.dataset.order;
    this.setData({
      confirmShow: true
    });
  },

  cancle() {
    this.setData({
      confirmShow: false
    });
  },
  sure() {
    let updateOrder = {
      orderId: this.data.currentOrderData.orderId,
      newStatus: 2
    };
    http.updateOrderStatus([updateOrder]).then(res => {
      if (res.code == 0) {
        this.setData({
          confirmShow: false
        });
        wx.showToast({
          duration: 2000,
          icon: 'none',
          title: '确认成功'
        })
        setTimeout(() => {
          this.setData({
            orderList: []
          });
          this.initData();
        }, 2000);

      } else {
        wx.showToast({
          duration: 1000,
          icon: 'none',
          title: res.msg
        })
      }
    })
  },
  clickAddress(e) {
    let order = e.currentTarget.dataset.order;
    Dialog.alert({
      title: order.schoolName,
      message: order.schoolAddr,
      confirmButtonColor: "#21af65",
    }).then(() => {
      // on close
    });
  },
  clickPhone(e) {
    let order = e.currentTarget.dataset.order;
    Dialog.confirm({
      title: order.schoolName,
      message: order.schoolTel + '\n负责人：' + order.adminName,
      showCancelButton: true,
      confirmButtonText: "拨打电话",
      confirmButtonColor: "#21af65",
      cancelButtonColor: "#21af65"
    }).then(() => {
      if (order.schoolTel && order.schoolTel !== "") {
        wx.makePhoneCall({
          phoneNumber: order.schoolTel //仅为示例，并非真实的电话号码
        })
      }
    }).catch(() => {
      // on cancel
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})